﻿#include <QCryptographicHash>
#include <QFile>
#include <QDebug>
#include "public.h"
#include "qaesencryption.h"

std::vector<int> constructMsgData(const std::vector<int>& di, int afn, const std::vector<int>& content) {
	static uint8_t seqNum = 0;
	seqNum++;
	std::vector<int> msg = { 0x68, 0x00, 0x00, 0x40 };
	msg.push_back(afn);
	msg.push_back(seqNum);
	msg.insert(msg.end(), di.begin(), di.end());
	msg.insert(msg.end(), content.begin(), content.end());
	msg.push_back(0x00);
	msg.push_back(0x16);
	msg[1] = msg.size();
	uint64_t crcSum = 0;
	for (int index = 2; index < msg.size() - 2; ++index) {
		crcSum += msg[index];
	}
	msg[msg.size() - 2] = crcSum & 0xFF;
	return msg;
}

QByteArray vectorToByteArray(const std::vector<int>& data) {
	QByteArray arrayData;
	for (auto&& d : data) {
		arrayData.append(d);
	}
	return arrayData;
}

QString convertToString(const QByteArray& data) {
	QString buffer;
	for (int i = 0; i < data.size(); i++)
	{
		QString s = QString::asprintf("0x%02x ", (unsigned char)data.at(i));
		buffer.append(s);
	}
	return buffer;
}

QString convertToStringToShow(const QByteArray& data) {
	QString buffer;
	for (int i = 0; i < data.size(); i++)
	{
		QString s = QString::asprintf("%02x", (unsigned char)data.at(i));
		buffer.append(s);
	}
	return buffer;
}

QString convertToString(uint32_t num) {
	QString buffer;
	QString s = QString::asprintf("0x%02x", (unsigned char)((num >> 24) & 0xFF));
	buffer.append(s);
	s = QString::asprintf("%02x", (unsigned char)((num >> 16) & 0xFF));
	buffer.append(s);
	s = QString::asprintf("%02x", (unsigned char)((num >> 8) & 0xFF));
	buffer.append(s);
	s = QString::asprintf("%02x", (unsigned char)(num & 0xFF));
	buffer.append(s);
	return buffer;
}

QString AESEncode(QString key, QString data) {
	QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);
	QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Md5);
	QByteArray encodedText = encryption.encode(data.toLocal8Bit(), hashKey);
	QString str_encode_text = QString::fromLocal8Bit(encodedText.toBase64());
	return str_encode_text;
}

QString AESDecode(QString key, QString data) {
	QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);
	QByteArray hashKey2 = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Md5);
	QByteArray decodedText = encryption.decode(QByteArray::fromBase64(data.toLocal8Bit()), hashKey2);
	auto findIt = decodedText.rend();
	for (auto it = decodedText.rbegin(); it != decodedText.rend(); ++it) {
		if (*it == '}') {
			findIt = it;
			break;
		}
	}
	if (findIt != decodedText.rend()) {
		decodedText.erase((findIt - 1).base(), decodedText.end());
	}
	return QString::fromLocal8Bit(decodedText);
}

bool writeRegisterInfo(QString content) {
	QFile fileRegisterInfo("register_info.key");
	if (!fileRegisterInfo.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
		qDebug() << "register_info.key open error";
		return false;
	}
	qDebug() << "write register_info.key open!";
	fileRegisterInfo.write(content.toLatin1());
	fileRegisterInfo.flush();
	fileRegisterInfo.close();
	return true;
}